# -*- coding: utf-8 -*-

__version__ = '0.0.1'
__author__  = '$zhangbo'
__time__    = '$2013-11-07 22:52:00'
__all__     = ['MongoDBOperation']

import datetime

from pymongo import Connection

from jpdog.config.mongo import MONGODB_DATABASES, MONGODB_DATABASE_NAME


class MongoDBOperation(object):
    '''MongoDB 操作单实例，考虑到MongoDB支持分布式，这里整个系统
    都使用一个连接到Mongos'''
    _MongoDB_  = None

    def __new__(cls, *arg, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super(MongoDBOperation, cls).__new__(cls, *args, **kwargs)  
        return cls._instance

    def __init__(self):
        if not MongoDBOperation._MongoDB_:
            MongoDBOperation._MongoDB_ = Connection(MONGODB_DATABASES)

    def getCursor(self, collection_name):
        return MongoDBOperation._MongoDB_.[collection_name]